{extend name="layouts/app"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">添加后台菜单</div>
    <div class="layui-card-body">
        <form class="layui-form" method="post" action="{:url('save')}">
            <div class="layui-form-item">
                <label class="layui-form-label">上级分类</label>
                <div class="layui-input-inline w300">
                    <cascader id="category"></cascader>
                    <input type="hidden" name="parent_id" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-inline w300">
                    <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入分类名称" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">分类描述</label>
                <div class="layui-input-inline w300">
                    <textarea name="description" placeholder="请输入分类描述" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">分类图片 </label>
                <div class="layui-input-inline w300">
                    <a id="seleBtn" class="layui-btn layui-btn-primary" href="javascript:;" data-url="{:url('uploads/image')}"  onclick="popUpload(this)">选择图标</a>
                    <input type="hidden" name="image">
                    <img src="" style="display:none; width:60px;height:60px">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="正常" checked >
                    <input type="radio" name="status" value="0" title="禁用" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否目录</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_directory" value="1" title="是" checked >
                    <input type="radio" name="is_directory" value="0" title="否" >
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="hidden" name="type" value="{$type}">
                    <button class="layui-btn" lay-submit="" lay-filter="formSubmit">立即提交</button>
                    <button class="layui-btn layui-btn-normal" type="button" onclick="javascript:history.back(-1);">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    layui.extend({
        ajaxCascader: 'ajaxCascader/ajaxCascader'
    }).use([ 'form', 'layer','ajaxCascader'], function () {
        var $ = layui.$,
            ajaxCascader=layui.ajaxCascader;

        var type = "{$type}";
        //选择菜单
        ajaxCascader.load({
            elem: '#category'
            ,search: {
                show: true,
                minLabel: 10,
                placeholder: '请输入搜索词'
            }
            ,clear: true
            ,value: 0
            ,getChildren: function(value,callback){
                    var data = [];
                    $.ajax({
                        url:"{:url('apiGetCategory')}",
                        type:'get',
                        data:{parent_id:value,type:type},
                        success:function(res){
                            data = res.data;
                            for(var i in data){
                                data[i].value = data[i].id;
                                data[i].label = data[i].title;
                                delete data[i].id;
                                delete data[i].title;
                                data[i].hasChild = data[i].is_directory;
                            }
                            callback(data);
                        }
                    });
                }
            });

        ajaxCascader.on('click', '#category', function(data){
            $("input[name='parent_id']").val(data.value);
        });

    });
</script>
{/block}